#include "stdafx.h"

#include "PhoneTbl.h"

CPhoneTable::CPhoneTable()
{
	m_sName = TABLE_NAME_PHONE;
}
bool CPhoneTable::checkIMEI(CString sIMEI,CPhone* &pPhone)
{
	CString sQuery;
	MYSQL_ROW row = NULL;
	mysql_query(conn,"SET character_set_results=utf8");
	sQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s'"),g_sDatabase,TABLE_NAME_PHONE,FIELD_IMEI_PHONE,sIMEI);
	CT2A ascii(sQuery, CP_UTF8);	
	mysql_query(conn,ascii.m_psz);
	MYSQL_RES * pResult = mysql_use_result(conn);
	bool bReturn = false;
	if (pResult)
	{
		row = mysql_fetch_row(pResult);
		if(row)
		{
			pPhone = new CPhone;
			pPhone->setCode(CString(row[0]));
			pPhone->setCodeProduct(CString(row[1]));
			pPhone->setIMEI(CString(row[2]));
			pPhone->setCodeStore(CString(row[3]));	
			pPhone->setPriceInput(CString(row[4]));
			pPhone->setPriceSell(CString(row[5]));
			pPhone->setPriceWholesale(CString(row[6]));
			CString sNote = CA2W(row[7], CP_UTF8);
			pPhone->setNote(sNote);
			pPhone->setStatus(atoi(row[8]));
			bReturn = true;
		}
		mysql_free_result(pResult);
	}
	return bReturn;
}

CPhone* CPhoneTable::getPhoneAtStoreByImei(CString sIMEI, CString sCodeStore)
{
	CPhone* pPhone = NULL;
	CString sQuery;
	MYSQL_ROW row = NULL;
	mysql_query(conn,"SET character_set_results=utf8");
	sQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s' AND %s = '%s'"),g_sDatabase,TABLE_NAME_PHONE,FIELD_IMEI_PHONE,sIMEI,FIELD_CODE_STORE,sCodeStore);
	CT2A ascii(sQuery, CP_UTF8);	
	mysql_query(conn,ascii.m_psz);
	MYSQL_RES * pResult = mysql_use_result(conn);	
	if (pResult)
	{
		row = mysql_fetch_row(pResult);
		if(row)
		{
			pPhone = new CPhone;
			pPhone->setCode(CString(row[0]));
			pPhone->setCodeProduct(CString(row[1]));
			pPhone->setIMEI(CString(row[2]));
			pPhone->setCodeStore(CString(row[3]));	
			pPhone->setPriceInput(CString(row[4]));
			pPhone->setPriceSell(CString(row[5]));
			pPhone->setPriceWholesale(CString(row[6]));
			CString sNote = CA2W(row[7], CP_UTF8);
			pPhone->setNote(sNote);
			pPhone->setStatus(atoi(row[8]));			
		}
		mysql_free_result(pResult);
	}
	return pPhone;
}

CPhone* CPhoneTable::getPhoneAtStoreByCode(CString sCodePhone, CString sCodeStore)
{
	CPhone* pPhone = NULL;
	CString sQuery;
	MYSQL_ROW row = NULL;
	mysql_query(conn,"SET character_set_results=utf8");
	sQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s' AND %s = '%s'"),g_sDatabase,TABLE_NAME_PHONE,FIELD_CODE_PHONE,sCodePhone,FIELD_CODE_STORE,sCodeStore);
	CT2A ascii(sQuery, CP_UTF8);	
	mysql_query(conn,ascii.m_psz);
	MYSQL_RES * pResult = mysql_use_result(conn);	
	if (pResult)
	{
		row = mysql_fetch_row(pResult);
		if(row)
		{
			pPhone = new CPhone;
			pPhone->setCode(CString(row[0]));
			pPhone->setCodeProduct(CString(row[1]));
			pPhone->setIMEI(CString(row[2]));
			pPhone->setCodeStore(CString(row[3]));	
			pPhone->setPriceInput(CString(row[4]));
			pPhone->setPriceSell(CString(row[5]));
			pPhone->setPriceWholesale(CString(row[6]));
			CString sNote = CA2W(row[7], CP_UTF8);
			pPhone->setNote(sNote);
			pPhone->setStatus(atoi(row[8]));			
		}
		mysql_free_result(pResult);
	}
	return pPhone;
}

bool CPhoneTable::updateTable(const CPhone* pPhone)
{
	bool bResult = false;
	if (pPhone)
	{
		CString sQuery;
		MYSQL_ROW row = NULL;
		sQuery.Format(_T("UPDATE %s.%s SET %s = '%s' ,%s='%s',%s='%s',%s= '%s',%s='%s',%s='%s',%s=_utf8'%s' WHERE %s ='%s'"),
			g_sDatabase,TABLE_NAME_PHONE,
			FIELD_CODE_PHONE,pPhone->getCode(),
			FIELD_CODE_PRODUCT,pPhone->getCodeProduct(),
			FIELD_CODE_STORE,pPhone->getCodeStore(),
			FIELD_PRICE_INPUT,pPhone->getPriceInput(),
			FIELD_PRICE_SELL,pPhone->getPriceSell(),
			FIELD_PRICE_WHOLESALE,pPhone->getPriceWholesale(),
			FIELD_NOTE_PHONE,pPhone->getNote(),
			FIELD_IMEI_PHONE,pPhone->getIMEI());
		CT2A ascii(sQuery, CP_UTF8);	
		if(mysql_query(conn,ascii.m_psz)==0)
			bResult = true;
	}
	return bResult;
}

bool CPhoneTable::updateTransfer(const CPhone* pPhone)
{
	bool bResult = false;
	if (pPhone)
	{
		CString sQuery;
		MYSQL_ROW row = NULL;
		sQuery.Format(_T("UPDATE %s.%s SET %s = '%s' WHERE %s ='%s'"),
			g_sDatabase,TABLE_NAME_PHONE,			
			FIELD_CODE_STORE,pPhone->getCodeStore(),			
			FIELD_CODE_PHONE,pPhone->getCode());
		CT2A ascii(sQuery, CP_UTF8);	
		if(mysql_query(conn,ascii.m_psz)==0)
			bResult = true;
	}
	return bResult;
}
bool  CPhoneTable::InsertPhone(const CPhone* pPhone)
{
	CString sValue;
	if (pPhone)
	{
		sValue.Format(_T("( '%s','%s', '%s', '%s','%s','%s','%s',_utf8'%s',%d)")
			,pPhone->getCode(),pPhone->getCodeProduct(),pPhone->getIMEI(),pPhone->getCodeStore(),
			pPhone->getPriceInput(),pPhone->getPriceSell(),pPhone->getPriceWholesale(),pPhone->getNote(),pPhone->getStatus());

		return insertRecord(TBL_PHONE,sValue);
	}
	return false;
}

 bool CPhoneTable::deleteRecord(CString sIMEI)
{
	CString sQuery;
	sQuery.Format(_T("DELETE  FROM %s.%s WHERE %s = '%s'"),g_sDatabase,TABLE_NAME_PHONE, FIELD_IMEI_PHONE,sIMEI) ;
	CT2A ascii(sQuery, CP_UTF8);	
	mysql_query(conn,ascii.m_psz);
	MYSQL_RES * pResult = mysql_use_result(conn);
	if (pResult)
		return true;
	return false;
 }

 CPhone*  CPhoneTable::getPhoneByCode(const CString& sCode)
 {
	 CPhone* pPhone = NULL;
	 MYSQL_RES * pResult = NULL;
	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	 sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s'"),g_sDatabase,TABLE_NAME_PHONE,FIELD_CODE_PHONE,sCode);
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = mysql_fetch_row(pResult);
		 if(row)
		 {
			 pPhone = new CPhone;
			 pPhone->setCode(CString(row[0]));
			 pPhone->setCodeProduct(CString(row[1]));
			 pPhone->setIMEI(CString(row[2]));
			 pPhone->setCodeStore(CString(row[3]));
			 pPhone->setPriceInput(CString(row[4])); 
			 pPhone->setPriceSell(CString(row[5]));
			 pPhone->setPriceWholesale(CString(row[6]));
			 CString sNote = CA2W(row[7], CP_UTF8);
			 pPhone->setNote(sNote);
			 pPhone->setStatus(atoi(row[8]));

		 }
		 mysql_free_result(pResult);
	 }
	 return pPhone;
 }
 vector<CPhone*>  CPhoneTable::getListPhone(const CString& sCodeProduct)
 {
	 vector<CPhone*> vtPhone;

	 MYSQL_RES * pResult = NULL;
	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	 sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s'"),g_sDatabase,TABLE_NAME_PHONE,FIELD_CODE_PRODUCT,sCodeProduct);
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = NULL;
		 while((row = mysql_fetch_row(pResult)) )
		 {
			 CPhone* pPhone = new CPhone;
			 pPhone->setCode(CString(row[0]));
			 pPhone->setCodeProduct(CString(row[1]));
			 pPhone->setIMEI(CString(row[2]));
			 pPhone->setCodeStore(CString(row[3]));
			 pPhone->setPriceInput(CString(row[4])); 
			 pPhone->setPriceSell(CString(row[5]));
			 pPhone->setPriceWholesale(CString(row[6]));
			 CString sNote = CA2W(row[7], CP_UTF8);
			 pPhone->setNote(sNote);
			 vtPhone.push_back(pPhone);
		 }
		 mysql_free_result(pResult);
	 }
	 return vtPhone;
 }
vector<CPhone*>  CPhoneTable::getListPhoneByStore(const CString& sCodeProduct, const CString& sCodeStore)
{
	vector<CPhone*> vtPhone;

	MYSQL_RES * pResult = NULL;
	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s' AND %s = '%s'"),g_sDatabase,TABLE_NAME_PHONE,FIELD_CODE_PRODUCT,sCodeProduct,FIELD_CODE_STORE,sCodeStore);
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = NULL;
		while((row = mysql_fetch_row(pResult)) )
		{
			CPhone* pPhone = new CPhone;
			pPhone->setCode(CString(row[0]));
			pPhone->setCodeProduct(CString(row[1]));
			pPhone->setIMEI(CString(row[2]));
			pPhone->setCodeStore(CString(row[3]));
			pPhone->setPriceInput(CString(row[4])); 
			pPhone->setPriceSell(CString(row[5]));
			pPhone->setPriceWholesale(CString(row[6]));
			CString sNote = CA2W(row[7], CP_UTF8);
			pPhone->setNote(sNote);
			vtPhone.push_back(pPhone);
		}
		mysql_free_result(pResult);
	}
	return vtPhone;
}
 int CPhoneTable::getCountPhone(const CString& sCodeProduct)
 {
	 int nCountNumber =0;
	 MYSQL_RES * pResult = NULL;
	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	 sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s'"),g_sDatabase,TABLE_NAME_PHONE,FIELD_CODE_PRODUCT,sCodeProduct);
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = NULL;
		 while((row = mysql_fetch_row(pResult)) )
		 {
			 nCountNumber ++;
		 }
		 mysql_free_result(pResult);
	 }
	 return nCountNumber;
 }
int CPhoneTable::getCountPhoneInStore(const CString& sCodeProduct,const CString& sCodeStore)
{
	int nNumber = 0;
	MYSQL_RES * pResult = NULL;
	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s' AND %s = '%s'"),g_sDatabase,TABLE_NAME_PHONE,FIELD_CODE_PRODUCT,sCodeProduct,FIELD_CODE_STORE,sCodeStore);
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = NULL;
		while((row = mysql_fetch_row(pResult)) )
		{
			nNumber ++;
		}
		mysql_free_result(pResult);
	}
	return nNumber;
}
bool CPhoneTable::getTypePhoneRemain(const CString& sCodeStore,vector<CString>& vtCodeProduct, vector<int>&vtCount)
{
	bool bRes = false;
	MYSQL_RES * pResult = NULL;
	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT %s , COUNT(%s) FROM %s.%s WHERE %s = '%s' AND %s !=1 GROUP BY %s "),FIELD_CODE_PRODUCT,FIELD_CODE_PHONE,g_sDatabase,TABLE_NAME_PHONE,FIELD_CODE_STORE,sCodeStore,FIELD_STATUS,FIELD_CODE_PRODUCT);
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = NULL;
		while((row = mysql_fetch_row(pResult)) )
		{
			vtCodeProduct.push_back(CString(row[0]));
			vtCount.push_back(atoi(row[1]));
		}
		bRes = true;
		 mysql_free_result(pResult);
	}
	return bRes;
}
 vector<CPhone*>  CPhoneTable::getListPhoneInBill(const CString& sCodeBill)
 {
	 vector<CPhone*> vtPhone;

	 MYSQL_RES * pResult = NULL;
	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	 sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s'"),g_sDatabase,TABLE_NAME_PHONE,FIELD_CODE_BILL_INPUT,sCodeBill);
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = NULL;
		 while((row = mysql_fetch_row(pResult)) )
		 {
			 CPhone* pPhone = new CPhone;
			 pPhone->setCode(CString(row[0]));
			 pPhone->setCodeProduct(CString(row[1]));
			 pPhone->setIMEI(CString(row[2]));
			 pPhone->setCodeStore(CString(row[3]));	
			 pPhone->setPriceInput(CString(row[4])); 
			 pPhone->setPriceSell(CString(row[5]));
			 pPhone->setPriceWholesale(CString(row[6]));
			 CString sNote = CA2W(row[7], CP_UTF8);
			 pPhone->setNote(sNote);
			  pPhone->setStatus(atoi(row[8]));
			 vtPhone.push_back(pPhone);
		 }
		 mysql_free_result(pResult);
	 }
	 return vtPhone;
 }
 int  CPhoneTable::getMaxCode()
 {
	 int nMax = 0;
	 MYSQL_RES * pResult = NULL;

	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	 sNewQuery.Format(_T("SELECT %s ,MAX(%s)FROM %s.%s "),FIELD_CODE_PHONE,FIELD_CODE_PHONE,g_sDatabase,TABLE_NAME_PHONE);
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = mysql_fetch_row(pResult);
		 if (row)
		 {
			 CString sTemplate;
			 sTemplate = row[1];
			 sTemplate.Replace(_T("DT"),_T("0"));
			 nMax = _tstoi(sTemplate);
		 }
		 mysql_free_result(pResult);
	 }
	 return nMax;
 }

bool CPhoneTable::buyPhone(CString sCodePhone)
{
	bool bResult = false;
	
	CString sQuery;
	MYSQL_ROW row = NULL;
	sQuery.Format(_T("UPDATE %s.%s SET %s = %d WHERE %s ='%s'"),
		g_sDatabase,TABLE_NAME_PHONE,
		FIELD_STATUS,1,
		FIELD_CODE_PHONE,sCodePhone);
	CT2A ascii(sQuery, CP_UTF8);	
	if(mysql_query(conn,ascii.m_psz)==0)
		bResult = true;
	
	return bResult;
}